<?php
class Delivery_Model_Negocio 
	extends Delivery_Model_Abstract
{
	public $idNegocio;
	public $email;
	public $nombre;
	public $telefono;
	public $abierto;	
	public $delivery;	
	public $pickup;	
	public $programado;	
	public $tiempoEntrega;
	public $url;
	public $ultimoPing;
	public $descripcion;
	public $activo;
	public $ratingEntrega;
	public $ratingCalidad;
	public $ratingPrecio;
	public $idDireccion;
	public $picasaAlbumId;
	public $ordenCategorias;
	public $notas;
	public $estado;
	
	protected $_direccion;
	protected $_productos;
	protected $_promociones;
	protected $_zonas;
	protected $_tags;
	protected $_horarios;
	protected $_votos;
	
	protected $_estados = array(
	
	);
	
	
	public function getId()
	{
		return $this->idNegocio;	
	}
	
	public function getDireccion()
	{
		return $this->_direccion;
	}
	
	public function setDireccion( $direccion )
	{
		$this->_direccion = $direccion;
		return $this;
	}
	
	public function getProductos()
	{
		return $this->_productos;
	}
	
	public function setProductos( $productos )
	{
		$this->_productos = $productos;
		return $this;
	}
	
	public function getPromociones()
	{
		return $this->_promociones;
	}
	
	public function setPromociones( $promociones )
	{
		$this->_promociones = $promociones;
		return $this;
	}
	
	public function getZonas()
	{
		return $this->_zonas;
	}
	
	public function setZonas( $zonas )
	{
		$this->_zonas = $zonas;
		return $this;
	}
	
	public function getTags()
	{
		return $this->_tags;
	}
	
	public function setTags( $tags )
	{
		$this->_tags = $tags;
		return $this;
	}
	
	public function getHorarios()
	{
		return $this->_horarios;
	}
	
	public function setHorarios( $horarios )
	{
		$this->_horarios = $horarios;
		return $this;
	}
	
    public function getVotos()
	{
		return $this->_votos;
	}
	
	public function setVotos( $votos )
	{
		$this->_votos = $votos;
		return $this;
	}
	
	public function getCocinas()
	{
		return $this->getTagsType( "Cuisine" );	
	}
	
	public function getComidas()
	{
		return $this->getTagsType( "Food" );	
	}
	
	public function getMediosDePago()
	{
		return $this->getTagsType( "Payment" );	
	}
	
	public function getBarrios()
	{
		return $this->getTagsType( "District" );	
	}
	
	public function getTagsType( $type )
	{
		$tags = $this->getTags();
		
		$types = array();
		
		$f = "is{$type}";
		
		foreach($tags as $tag){
			if( $tag->$f()){
				$types[] = $tag;
			}
		}
		
		return	$types;	
	}
	
    public function getCiudad()
	{
		return $this->getDireccion()->getCiudad();	
	}
		
	public function esClienteEspecial( $cliente )
	{
		return false;
	}

	public function estaAbierto()
	{
		return $this->abierto;
	}
	
	public function tieneFotos()
	{
		return ! is_null($this->picasaAlbumId);
	}
	
	public function getAlbumId()
	{
		return $this->picasaAlbumId;
	}
	
	public function estaOnline()
	{
		$online = false;
		
		$now = Zend_Date::now();
		$now->subMinute(1);
//		$ultimoPing = new Zend_Date( $this->ultimoPing );
		
//		if ( $ultimoPing->isLater( $now, Zend_Date::DATETIME ) && $this->abreHoy() ){
		if ( $this->abreHoy() ){
			
			$horarios = $this->horariosHoy();
			
			foreach ( $horarios as $horario){
			
				$desde = $horario->desde();
				$hasta = $horario->hasta();
				
				if ( $now->isLater( $desde, Zend_Date::DATETIME ) &&  $hasta->isLater( $now, Zend_Date::DATETIME )){
					$online = true;
					break;
				}
			}
			
		} 
		
		return $online;
	}
	
	public function abreHoy( )
	{
		$horarios = $this->horariosHoy();
		
		return ! empty($horarios);
	}
	
    public function horariosWeek( $type = 'delivery' )
	{
		$horariosWeek = array();
		$horariosWeekFinal = array();
	
		$horarios = $this->getHorarios();
		
		foreach($horarios as $horario){
		
		    list($dayName, $desde, $hasta) = $horario->getNextDate();
		    
		    if (!isset( $horariosWeek[$dayName] )){
		        $horariosWeek[$dayName] = array();
		    }
		    
		    $horariosWeek[$dayName][] =  array(
		        'desde' => $desde,
		        'hasta' => $hasta
		    );
		}
		
		ksort( $horariosWeek );
		
		/******************/
		
		$now = Zend_Registry::get( "now" );
		
		$entregaFrom = $this->_entregaFrom(); 
		$entregaDiff = $this->_entregaDiff();
		
		$min = current(current($horariosWeek));
		$min = clone $min['desde'];
    	$min->addMinute( $entregaFrom );
		
		
		foreach ( $horariosWeek as $date => $horariosDay ){
		
		    $saltoRange	= $this->_saltoRange( $horariosDay, $now);
		
    		foreach($horariosDay as $horarios){
    			
    			$desde = $horarios['desde'];
    			$hasta = $horarios['hasta'];
    			
    			$firts = clone $desde;
    			$firts->addHour(1);
    					
    			if ( $firts->isLater( $min ) ){
    				$min = $firts;
    			}
    			
    		    if ( $min->isLater( $hasta ) ){
    				continue;
    			}
    			
    			while ( $desde->compare( $hasta ) < 0 ){
    				
    				
    				$diffNow = (int) (($desde->getTimestamp() - $now->getTimestamp()) / 60);
    				
    				
    				if ( $desde->compare( $min ) >= 0 && $diffNow >= $entregaFrom ){
    					
    					$k = $desde->getTimestamp();
    					
    					$horariosWeekFinal[$date][$k]= $desde->get( Zend_Date::TIME_SHORT );
    					
    					
    					if( $type == 'delivery' && $entregaDiff){
    					    
    					    $toRange = clone $desde;
				            $toRange->addMinute( $entregaDiff );
				            
				            $horariosWeekFinal[$date][$k].= ' - ' . $toRange->get( Zend_Date::TIME_SHORT ) ;
				            
    					}
    				} 
    				
    				$desde->addMinute( $saltoRange );
    				
    			}
    		}
		}
		
		
		return $horariosWeekFinal;
	}
	
	public function horariosHoy()
	{
		$horariosHoy = array();
		$horarios = $this->getHorarios();
		
		foreach($horarios as $horario){
		
			if ($horario->isToday()){
				$horariosHoy[]= $horario;
			}
		}
		
		return $horariosHoy;
	}
	
	public function horariosDeliveryHoy()
	{
		$horariosDeliveryHoy = array();
		$horariosHoy = $this->horariosHoy();
		
		if( empty($horariosHoy)){
			return $horariosDeliveryHoy;
		}
		
		$now = Zend_Date::now();
		
		$entregaFrom= $this->_entregaFrom(); 
		$entregaDiff= $this->_entregaDiff(); 
		$saltoRange	= $this->_saltoRange( $horariosHoy, $now); 
		
		foreach($horariosHoy as $horarios){
			
			$desde = $horarios->desde();
			$hasta = $horarios->hasta();
			
			$min = clone $desde;
			$min->addMinute( $entregaFrom );
			
			$firts = clone $desde;
			$firts->addHour(1);
					
			if ( $firts->isLater( $min ) ){
				$min = $firts;
			}
			
			while ( $desde->compare( $hasta ) < 0 ){
				
				$toRange = clone $desde;
				$toRange->addMinute( $entregaDiff );
				
				$diffNow = ($desde->getTimestamp() - $now->getTimestamp()) / 60;
				
				
				if ( $desde->compare( $min ) >= 0 && $diffNow >= $entregaFrom){
//				if ( $desde->compare( $min ) >= 0 && $diffNow >= $entregaFrom && $toRange->compare( $hasta ) <= 0){
					
					$k = $desde->getTimestamp();
					
					$horariosDeliveryHoy[$k]= $desde->get( Zend_Date::TIME_SHORT ) 
											. ' - '
											. $toRange->get( Zend_Date::TIME_SHORT ) ;
					
				} 
				
				$desde->addMinute( $saltoRange );
				
			}
			
		}
		
		return $horariosDeliveryHoy;
	}
	
	public function horariosPickupHoy()
	{
		$horariosDeliveryHoy = array();
		$horariosHoy = $this->horariosHoy();
		
	    if( empty($horariosHoy)){
			return $horariosDeliveryHoy;
		}
		
		$now = Zend_Date::now();
		
		$entregaFrom = $this->_entregaFrom(); 
		$entregaDiff = $this->_entregaDiff();
		$saltoRange	= $this->_saltoRange( $horariosHoy, $now);
		
		foreach($horariosHoy as $horarios){
			
			$desde = $horarios->desde();
			$hasta = $horarios->hasta();
			
			$min = clone $desde;
			$min->addMinute( $entregaFrom );
			
			$firts = clone $desde;
			$firts->addHour(1);
					
			if ( $firts->isLater( $min ) ){
				$min = $firts;
			}
			
			while ( $desde->compare( $hasta ) < 0 ){
				
				
				$diffNow = ($desde->getTimestamp() - $now->getTimestamp()) / 60;
				
				if ( $desde->compare( $min ) >= 0 && $diffNow >= $entregaFrom ){
					
					$k = $desde->getTimestamp();
					
					$horariosDeliveryHoy[$k]= $desde->get( Zend_Date::TIME_SHORT );
					
				} 
				
				$desde->addMinute( $saltoRange );
				
			}
		}
		
		return $horariosDeliveryHoy;
	}
	
	public function direccionEnZona( $idDireccion ){
		$NegociosDbTable = Delivery_Model_Factory::dbTable('Negocios');
		$rowset = $NegociosDbTable->direccionEnZona( $this->idNegocio, $idDireccion );
		return $rowset;
	}
	
	public function checkEstado(){
	
	    $estado = 4;
		
		if ($this->estaAbierto()){
		
			if ($this->estaOnline()){
				
				$estado = 1;
				
			} elseif( $this->abreHoy()){
					
					
				$horariosHoy = $this->horariosHoy();
				
				$now = new Zend_Date();
				
				foreach($horariosHoy as $horarioHoy){
				
					$desde = $horarioHoy->desde();
					
					if( $now->isEarlier( $desde )){
						
						$estado = 2;
						break;
						
					} elseif( $now->isLater( $horarioHoy->hasta() ) ) {
						$estado = 3;
					}
				}
		
			}
		} else {
		    $estado = 4;
		}
	    
	    return $estado;
	    
	}
	
	protected function _entregaFrom()
	{
		return strstr($this->tiempoEntrega, "-") ? current(explode("-", $this->tiempoEntrega)) : $this->tiempoEntrega;
	}
	
	protected function _entregaTo()
	{
		return strstr($this->tiempoEntrega, "-") ? end(explode("-", $this->tiempoEntrega)) : null;
	}
	
	protected function _entregaDiff()
	{
	    $to = $this->_entregaTo();
	    
	    if($to){
	        return $this->_entregaTo() - $this->_entregaFrom();
	    } else {
	        return 0;
	    }
		 
	}
	
	protected function _saltoRange( $horariosHoy, $now )
	{
		$ultimoHorario = end($horariosHoy);
		$ultimoHorario = is_array($ultimoHorario) ? $ultimoHorario['hasta'] : $ultimoHorario->hasta();
		
		$diffWithLast = (int) ($ultimoHorario->getTimestamp() - $now->getTimestamp()) / 60;
		
		if ( $diffWithLast > 60){
			$saltoRange = 15;
		}elseif ( $diffWithLast > 30){
			$saltoRange = 10;
		}else{
			$saltoRange = 5;
		}
		
		return $saltoRange;
	}
	
}
